package cn.shuto.liferay.plugin.chart.service.service.impl;

import java.util.Date;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;

import cn.shuto.liferay.plugin.chart.service.model.ChartTemplate;
import cn.shuto.liferay.plugin.chart.service.service.base.ChartTemplateLocalServiceBaseImpl;

/**
 * The implementation of the chart template local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link cn.shuto.liferay.plugin.chart.service.service.ChartTemplateLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author shixw
 * @see cn.shuto.liferay.plugin.chart.service.service.base.ChartTemplateLocalServiceBaseImpl
 * @see cn.shuto.liferay.plugin.chart.service.service.ChartTemplateLocalServiceUtil
 */
public class ChartTemplateLocalServiceImpl
    extends ChartTemplateLocalServiceBaseImpl {
	
	public ChartTemplate addTemplate(String name,String description,String script,String templateParam,ServiceContext serviceContext) throws SystemException,
		PortalException {
		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();
		long chartTemplateId = counterLocalService.increment();
		
		ChartTemplate template = this.chartTemplatePersistence.create(chartTemplateId);
		template.setUuid(serviceContext.getUuid());
		template.setUserId(userId);
		template.setGroupId(groupId);
		template.setCompanyId(user.getCompanyId());
		template.setUserName(user.getFullName());
		template.setCreateDate(serviceContext.getCreateDate(now));
		template.setModifiedDate(serviceContext.getModifiedDate(now));
		
		template.setName(name);
		template.setDescription(description);
		template.setScript(script);
		template.setTemplateParam(templateParam);
		
		chartTemplatePersistence.update(template);
		
		return template;
	}
	
	public ChartTemplate updateTemplate(long chartTemplateId,String name,String description,String script,String templateParam,ServiceContext serviceContext) throws SystemException,
		PortalException {
		long groupId = serviceContext.getScopeGroupId();
		long userId = serviceContext.getUserId();
		User user = userPersistence.findByPrimaryKey(userId);
		Date now = new Date();
		
		ChartTemplate template = getChartTemplate(chartTemplateId);
		
		template.setUserId(userId);
		template.setGroupId(groupId);
		template.setCompanyId(user.getCompanyId());
		template.setUserName(user.getFullName());
		template.setModifiedDate(serviceContext.getModifiedDate(now));
		
		template.setName(name);
		template.setDescription(description);
		template.setScript(script);
		template.setTemplateParam(templateParam);
		
		chartTemplatePersistence.update(template);
		
		return template;
}
}
